package com.example.administrator.my2048game;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 游戏主界面
 */
public class MainActivity extends AppCompatActivity {
    private TextView tv_score; // 计分
    private TextView tv_bestScore; // 计历史最高分
    public int score = 0; // 分数
    private int bestScores = 0; // 历史最高分
    private Button btn_tryAgain, btn_exitGame;
    private static MainActivity mainActivity = null;

    public MainActivity() {
        mainActivity = this;
    }

    public static MainActivity getMainActivity() {
        return mainActivity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置状态栏透明，使得状态栏的颜色与 app 顶部颜色一样
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        setContentView(R.layout.activity_main);
        initGame();
    }

    public void initGame() {
        tv_bestScore = findViewById(R.id.tv_bestScore);
        tv_score = findViewById(R.id.tv_score);
        btn_tryAgain = findViewById(R.id.btn_tryAgain);
        btn_exitGame = findViewById(R.id.btn_exitGame);
        btn_tryAgain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
                alertDialog.setTitle("My2048").setMessage("确定要重来一次吗？").setIcon(R.drawable.my2048game).setCancelable(false);
                alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        clearScore();
                        GameView.getGameView().startGame();
                        dialog.dismiss();
                    }
                });
                alertDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                alertDialog.show();
            }
        });
        btn_exitGame.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
                alertDialog.setTitle("My2048").setMessage("确定要退出游戏吗？").setIcon(R.drawable.my2048game).setCancelable(false);
                alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        System.exit(0);
                    }
                });
                alertDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                alertDialog.show();
            }
        });
        showScore();
        BestScore bestScore = new BestScore(this);
        bestScores = bestScore.getBestScore();
        tv_bestScore.setText(bestScores + "");
    }

    public void clearScore() {
        score = 0;
        showScore();
    }

    public void showScore() {
        tv_score.setText(score + "");
    }

    public void addScore(int s) {
        score += s;
        showScore();
        if (score > bestScores) {
            bestScores = score;
            BestScore bs = new BestScore(this);
            bs.setBestScore(bestScores);
            tv_bestScore.setText(bestScores + "");
        }
    }

    /**
     * 菜单、返回键响应(连续点击两次返回键退出程序)
     */
    private long exitTime = 0;
 
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(this, "再按一次退出My2048", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}